package com.hspedu.newData.dmsxl.回溯.组合;

import org.junit.Test;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-26 
 * @Version: 1.0
 */

public class LC39组合总和 {
    LinkedList<Integer> path = new LinkedList<>();
    List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        backtracking(candidates,target,0);
        return res;
    }

    // todo 剪枝优化如何操作
    public void backtracking(int[] candidates,int target,int startIndex){
        if (target < 0){
            return;
        }
        if (target == 0){
            res.add(new ArrayList<>(path));
            return;
        }

        for (int i = startIndex; i < candidates.length; i++) {
            path.add(candidates[i]);
            backtracking(candidates,target - candidates[i],i);
            path.removeLast();
        }
    }

    @Test
    public void test() {
        // 测试代码
        List<List<Integer>> list = combinationSum(new int[]{2, 3, 5}, 8);
        System.out.println("======");
    }
}
